package com.innjia.sys.service.impl;

import com.innjia.base.page.Page;
import com.innjia.base.page.PageHelper;
import com.innjia.base.utils.Query;
import com.innjia.sys.dao.UserLandlordDao;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.entity.UserLandlordEntity;
import com.innjia.sys.service.UserLandlordService;
import com.innjia.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("userLandlordService")
public class UserLandlordServiceImpl extends UserServiceImpl implements UserLandlordService {
    @Autowired
    private UserLandlordDao userLandlordDao;

    @Override
    public UserLandlordEntity queryObject(String id) {
        return userLandlordDao.queryObject(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(UserEntity user) {
        UserLandlordEntity userLandlord = (UserLandlordEntity) user;
        if (null == userLandlord.getLoginName()) {
            userLandlord.setLoginName(userLandlord.getPhone());
        }
        super.save(userLandlord);
        userLandlord.setUserId(userLandlord.getId());
        userLandlordDao.save(userLandlord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UserEntity user) {
        UserLandlordEntity userLandlord = (UserLandlordEntity) user;
        super.update(userLandlord);
        userLandlordDao.update(userLandlord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        super.delete(id);
        userLandlordDao.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(String[] ids) {
        super.deleteBatch(ids);
        userLandlordDao.deleteBatch(ids);
    }

    @Override
    public UserEntity queryByUid(String uid, String loginSrc) {
        UserLandlordEntity entity = new UserLandlordEntity();
        UserUtils.setUid(entity, uid, loginSrc);
        return userLandlordDao.queryByUid(entity);
    }

    @Override
    public UserEntity queryByPhone(String phone) {
        return userLandlordDao.queryByPhone(phone);
    }

    @Override
    public Page queryList(Map<String, Object> map) {
        Query query = (Query) map;
        PageHelper.startPage(query.getPage(), query.getLimit());
        userLandlordDao.queryList(map);
        return PageHelper.endPage();
    }

    @Override
    public UserEntity queryByLoginName(String logName) {
        return userLandlordDao.queryByLoginName(logName);
    }

}
